<?php 
namespace Home\Model;
use Think\Model;

class UserProvinceModel extends Model
{
	protected $trueTableName = "user_province";

	/*
	 * 获取到省份id
	 */
	public function get_province($userid,$page,$rows,$sort,$order,$program_name,$province_name,$idOnly=false)
	{
		$ids = [];
		$province_ids = $this->where(array("user_id"=>$userid))->field("province_id")->select();
		foreach ($province_ids as $key => $value) {
			$ids[] = $value['province_id']; 
		}
		if($idOnly){
			return $ids;
		}
		// dump($ids);exit();
		return D("Province")->get_use_province($ids,$page,$rows,$sort,$order,$program_name,$province_name,true);
	}

	public function provinceFilter($user_list,$province){
		$user_pro = $this->where(['user_id'=>['in',array_keys($user_list)]])->select();
		$province_ids = array_keys($province);
		foreach ($user_pro as $key => $value) {
			if(!in_array($value['province_id'], $province_ids)){
				unset($user_list[$value['user_id']]);
				continue;
			}
			if(isset($user_list[$value['user_id']])){
				$user_list[$value['user_id']]['province'][] = $province[$value['province_id']]['province'];
			}
		}
		foreach ($user_list as $key => $value) {
			$user_list[$key]['province'] = implode($user_list[$key]['province'], ',');
		}
		$user_list = array_merge($user_list);
		return $user_list;
	}

	public function getUserProvince($uid)
	{
		$province = $this->alias('a')->join('t_province as b on a.province_id = b.id')->where(['user_id'=>$uid])->field('province_id as id,province,status')->select(['index'=>'id']);
		return $province;
	}
}